package com.spx.chapter05.sink;

import com.spx.chapter05.pojo.Event;
import com.spx.util.KafkaUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.util.Properties;

/**
 * create by undeRdoG on  2022-05-01  14:25
 * 凡心所向，素履以往，生如逆旅，一苇以航。
 */
public class KafkaSinkTest {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


        Properties kafkaConsumerProperties = KafkaUtil.getKafkaCunsumerProperties("hadoop102:9092", "flink-kafka-source");

        DataStreamSource<String> clicks = env.addSource(new FlinkKafkaConsumer<String>(
                "flink-kafka-source",
                new SimpleStringSchema(),
                kafkaConsumerProperties));


        SingleOutputStreamOperator<Event> mapClicks = clicks.map(new MapFunction<String, Event>() {
            @Override
            public Event map(String value) throws Exception {
                //   Mary, ./home, 1000
                String[] fields = value.split(",");
                return new Event("name: " + fields[0].trim(), "url: " + fields[1].trim(), Long.parseLong(fields[2].trim()));
            }
        });

        SingleOutputStreamOperator<String> mapClicksToString = mapClicks.map(Event::toString);


        mapClicksToString.addSink(new FlinkKafkaProducer<String>("hadoop102:9092", "flink-kafka-sink", new SimpleStringSchema()));

        env.execute();


    }
}
